<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/8 0008
 * Time: 上午 9:55
 */

namespace Home\Controller;
use Think\Controller;

header("Content-type:text/html;charset=utf-8");
class UserController extends Controller{
    function user(){//用户主页
        $this->display();
    }

    function login(){//登陆
        if(!empty($_POST)){
            $user = D("user");
            $u_name = I('u_name');
            $psd = I('u_psd');
            $ret =$user ->where("u_name = '$u_name' and psd = '$psd'")->find();
            if($ret == null)
                echo "用户名或密码错误";
            else{
                $cartAmount = D("shop_car");
                $uid = $ret['u_id'];
                $Amount = $cartAmount->where("u_id = $uid")->count();
                session("amount",$Amount);
                session("u_name",$ret['u_name']);
                session("u_id",$ret['u_id']);
                $this -> redirect('User/user');
            }
        }
        else{
            $this->display();
        }
    }

    function logout(){//退出登陆
        session(null);
        $this->redirect('User/login');
    }

    function Regist(){//注册
        $user = D("User");
        if(!empty($_POST)){
            $verify = new \Think\Verify();
            if($verify->check($_POST['code'])) {
                $user->create();
                $rst = $user->add();
                if($rst){
                    $cartAmount = D("shop_car");
                    $uid = $rst;
                    $Amount = $cartAmount->where("u_id = $uid")->count();
                    session("amount",$Amount);
                    session("u_name",$_POST['u_name']);
                    session("u_id",$uid);
                    $this -> redirect('Index/index');
                }
            }
            else{
                echo '验证码错误';
                exit();
            }
        }
        else{
            $this->display();
        }
    }

    function personInfo(){//个人信息
        $u_id = I('u_id');
        $person = D("user");
        if(I('type')=="update"){
            $data['nick_name'] = I('nick_name');
            $data['u_phone'] =  I('u_phone');
            $data['sign'] =  I('sign');
            $data['email'] =  I('email');
            $person->where("u_id = $u_id")->save($data);
            $personInfo = $person->where("u_id = $u_id")->select();
            $this->assign("personInfo",$personInfo);
            $this->display();
        }
        else if(I('type')=="show") {
            $personInfo = $person->where("u_id = $u_id")->select();
            $this->assign("personInfo",$personInfo);
            $this->display();
        }
    }

    function verifyImg(){//验证码
        $config = array(
            'imageH'    =>  35,               // 验证码图片高度
            'imageW'    =>  140,               // 验证码图片宽度
            'length'    =>  4,               // 验证码位数
            'fontttf'   =>  '1.ttf',              // 验证码字体，不设置随机获取
            'fontSize'  =>  20,              // 验证码字体大小(px)
            'useCurve'  =>  false,          //
        );
        $verify = new \Think\Verify($config);
        $verify ->entry();
    }

    function contact(){//联系我们
        $this->display();
    }
}